package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.HealthVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.config.Oauth2Constant;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.constant.MongoDbConstant;
import com.bbzn.device.client.dataobject.DeviceConnectLog;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wulongbo
 * @Date 2022/2/11 17:11
 * @Version 1.0
 */

@RestController
@RequestMapping("/log")
@Slf4j
@Api(value = "log", tags = "连接日志管理")
public class ConnectLogController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private SimpleMongoService simpleMongoService;

    @Autowired
    private DeviceProcessService deviceProcessService;


    /**
     * 查询设备连接日志
     *
     * @param reqVO
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getDeviceConnectLog", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询设备连接日志")
    public BaseResponse getDeviceConnectLog(@RequestBody HealthVO reqVO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (reqVO.getPageNum() == null) {
            reqVO.setPageNum(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(100);
        }
        Map<String, Object> conditions = new HashMap();
        if (StringUtils.isNotEmpty(reqVO.getImei())) {
            conditions.put("clientid", Oauth2Constant.CLIENT_PRE + reqVO.getImei());
            PageInfo<DeviceConnectLog> page = simpleMongoService.selectPageByCondition(MongoDbConstant.DEVICE_CONNECT_LOG, null,
                    conditions, null, DeviceConnectLog.class, reqVO.getPageNum(), reqVO.getPageSize());
            return setResultSuccess(page);
        }

        List<String> list = deviceProcessService.findByImeisByOperatorId(userId);
        List<String> list2 = deviceService.findByAllImeis(userId);
        list.addAll(list2);
        if (OptionalBean.ofNullable(reqVO.getImei()).isPresent() && !list.contains(reqVO.getImei())) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_IMEI_NOT_EXIST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        list = list.stream().map(item -> {
            item = Oauth2Constant.CLIENT_PRE + item;
            return item;
        }).collect(Collectors.toList());

        Map<String, List<String>> inList = new HashMap();
        inList.put("clientid", list);

        PageInfo<DeviceConnectLog> page = simpleMongoService.selectPageByCondition(MongoDbConstant.DEVICE_CONNECT_LOG, null,
                conditions, inList, DeviceConnectLog.class, reqVO.getPageNum(), reqVO.getPageSize());
        return setResultSuccess(page);
    }
}
